package com.klikmee.sms.chat.web.usecases;

import java.util.ArrayList;
import org.json.JSONException;
import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.SignatureDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Signature;

public class SignatureDetailsForm extends ClientApplication {

	private ObjectDataSource dsSignature;
	private ArrayList<String> bcSignature;
	
	private Signature signature;
	private boolean editSignature;
	
	public void load() throws Exception {
		super.load();
		
		this.dsSignature = new ObjectDataSource("dsSignature");
		this.getDataSources().add(this.dsSignature);

		this.bcSignature = new ArrayList<String>();
		this.bcSignature.add("Name");
		this.bcSignature.add("Signature");
		this.bcSignature.add("Active");

		this.triggers.add(new Trigger(this, "btnSaveClick"));
		this.triggers.add(new Trigger(this, "btnCancelClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();
		
		this.dsSignature.setSource(signature);
		this.dsSignature.bind(bcSignature);
	}

	public void btnSaveClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		SignatureDAO signatureDAO = SpringBeansLookup.getBean(SignatureDAO.class);

		signature.setName(((Signature) this.dsSignature.getSource()).getName());
		signature.setSignature(((Signature) this.dsSignature.getSource()).getSignature());
		signature.setActive(((Signature) this.dsSignature.getSource()).getActive());

		if (editSignature) {
			signatureDAO.edit(signature);
		} else {
			signatureDAO.create(signature);
		}
		
		this.hide();
	}

	public void btnCancelClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		this.hide();
	}

	public void setSignature(Signature signature) {
		this.signature = signature;
	}

	public Signature getSignature() {
		return signature;
	}

	public void setEditSignature(boolean editSignature) {
		this.editSignature = editSignature;
	}

	public boolean getEditSignature() {
		return editSignature;
	}
}
